package org.br.erp.rfq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.br.erp.base.service.IScmbillcoderuleService;
import org.br.erp.base.utils.StringUtils;
import org.br.erp.rfq.entity.Scminquiry;
import org.br.erp.rfq.entity.Scminquiryitem;
import org.br.erp.rfq.entity.Scminquiryitemlinkvendor;
import org.br.erp.rfq.mapper.ScminquiryitemMapper;
import org.br.erp.rfq.mapper.ScminquiryMapper;
import org.br.erp.rfq.service.IScminquiryService;
import org.br.erp.rfq.service.IScminquiryitemlinkvendorService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 询价单
 * @Author: jeecg-boot
 * @Date:   2023-01-13
 * @Version: V1.0
 */
@Service
public class ScminquiryServiceImpl extends ServiceImpl<ScminquiryMapper, Scminquiry> implements IScminquiryService {

	@Autowired
	private ScminquiryMapper scminquiryMapper;
	@Autowired
	private ScminquiryitemMapper scminquiryitemMapper;

	@Autowired
	private IScmbillcoderuleService scmbillcoderuleService;

	@Autowired
	private IScminquiryitemlinkvendorService scminquiryitemlinkvendorService;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(Scminquiry scminquiry, List<Scminquiryitem> scminquiryitemList) {
		String billCode = scmbillcoderuleService.getBillCode("inquiry");
		if(StringUtils.isNotEmpty(billCode)){
			scminquiry.setBillcode(billCode);
		}
		scminquiryMapper.insert(scminquiry);
		//todo 获取要询价的供应商
		String vendorlinkid = scminquiry.getVendorlinkid();
		String[] vendorids = vendorlinkid.split(",");
		if(scminquiryitemList!=null && scminquiryitemList.size()>0) {
			for(Scminquiryitem entity:scminquiryitemList) {
				//外键设置
				entity.setParentid(scminquiry.getId());
				scminquiryitemMapper.insert(entity);
				if(vendorids!=null && vendorids.length > 0){
					for (String vendorid : vendorids) {
						generateInquiryItemLinkVendorSaveMethod(scminquiry, entity, vendorid);
					}
				}
			}
		}
	}

	private void generateInquiryItemLinkVendorSaveMethod(Scminquiry scminquiry, Scminquiryitem entity, String vendorid) {
		//先删除原始的信息
		QueryWrapper<Scminquiryitemlinkvendor> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("inquiryitemid",entity.getId());
		queryWrapper.eq("vendorid",vendorid);
		scminquiryitemlinkvendorService.remove(queryWrapper);
		//再新增
		Scminquiryitemlinkvendor scminquiryitemlinkvendor=new Scminquiryitemlinkvendor();
		scminquiryitemlinkvendor.setCompanyid(scminquiry.getCompanyid());
		scminquiryitemlinkvendor.setMaterilid(entity.getMaterialid());
		scminquiryitemlinkvendor.setInquiryitemid(entity.getId());
		scminquiryitemlinkvendor.setQty(entity.getQty());
		scminquiryitemlinkvendor.setQuoteflag("0");
		scminquiryitemlinkvendor.setRequiredate(entity.getRequiredate());
		scminquiryitemlinkvendor.setRequestdate(entity.getRequestdate());
		scminquiryitemlinkvendor.setSelfconfirmflag("0");
		scminquiryitemlinkvendor.setVendorconfirmflag("0");
		scminquiryitemlinkvendor.setVendorprice("0");
		scminquiryitemlinkvendor.setVendorqty("0");
		scminquiryitemlinkvendor.setQuoteflag("0");
		scminquiryitemlinkvendor.setVendorid(vendorid);
		scminquiryitemlinkvendor.setValiddate(scminquiry.getValiddate());
		scminquiryitemlinkvendorService.save(scminquiryitemlinkvendor);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(Scminquiry scminquiry,List<Scminquiryitem> scminquiryitemList) {
		scminquiryMapper.updateById(scminquiry);

		//1.先删除子表数据
		scminquiryitemMapper.deleteByMainId(scminquiry.getId());

		String vendorlinkid = scminquiry.getVendorlinkid();
		String[] vendorids = vendorlinkid.split(",");

		//2.子表数据重新插入
		if(scminquiryitemList!=null && scminquiryitemList.size()>0) {
			for(Scminquiryitem entity:scminquiryitemList) {
				//外键设置
				entity.setParentid(scminquiry.getId());
				scminquiryitemMapper.insert(entity);
				for (String vendorid : vendorids) {
					generateInquiryItemLinkVendorSaveMethod(scminquiry,entity,vendorid);
				}
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		scminquiryitemMapper.deleteByMainId(id);
		scminquiryMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			scminquiryitemMapper.deleteByMainId(id.toString());
			scminquiryMapper.deleteById(id);
		}
	}
	
}
